package queuedemo;
//用链表实现队列
public class MyQueue {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public int usedSize;
    public ListNode first;//队头
    public ListNode last;//队尾
    //尾插法
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(first == null) {
            first = node;
            last = node;
            usedSize++;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
            usedSize++;
        }
    }
    //删除头结点
    public void poll(){
        if(first == null) {
            return;
        }
        first = first.next;
        if(first == null) {
            last = null;
        }else {
            first.prev = null;
        }
        usedSize--;
    }
    public int peek() {
        if(first == null) {
            return -1;
        }else {
            return first.val;
        }
    }
    public int getSize() {
        return usedSize;
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
}
